﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web_Project
{
    public partial class test6_3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListBox2.DataBind();
                DropDownList2.DataBind();
                DropDownList3.DataBind();
                DropDownList4.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(TextBox1.Text))
            {
                try
                {
                    SqlDataSource2.Insert();
                    TextBox1.Text = string.Empty;
                    ListBox2.DataBind();
                    Label1.Text = "添加分类成功！";
                }
                catch(Exception ex)
                {
                    //处理错误
                    Label1.Text = "添加分类出错："+ex.Message;
                }
            }
            else
            {
                Label1.Text = "请输入需要添加的分类！";
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if(ListBox2.SelectedValue != null)
            {
                try
                {
                    SqlDataSource2.Delete();
                    TextBox1.Text = string.Empty;
                    ListBox2.DataBind();
                    Label1.Text = "删除成功！";
                }
                catch(Exception ex)
                {
                    //处理错误
                    Label1.Text = "删除失败：" + ex.Message;
                }
            }
            else
            {
                Label1.Text = "请选择需要删除的分类！";
            }
        }

        protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //当选择ListBox中的项目时触发
            if (ListBox2.SelectedValue != null)
            {
                TextBox1.Text = ListBox2.SelectedItem.Text;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if(ListBox2.SelectedValue != null && !string.IsNullOrEmpty(TextBox1.Text))
            {
                try
                {
                    SqlDataSource2.Update();
                    TextBox1.Text = string.Empty;
                    ListBox2.DataBind();
                    Label1.Text = "修改成功！";
                }
                catch (Exception ex)
                {
                    Label1.Text = "修改分类失败：" + ex.Message;
                }
            }
            else
            {
                if (ListBox2.SelectedValue != null)
                {
                    Label1.Text = "请选择要修改的分类！";
                }
                else if (string.IsNullOrEmpty(TextBox1.Text))
                {
                    Label1.Text = "请输入新的分类名称！";
                }
            }
        }

        //清空输入框的辅助方法
        private void ClearInputs()
        {
            TextBox2.Text = string.Empty;
            TextBox3.Text = string.Empty;
            TextBox4.Text = string.Empty;
            TextBox5.Text = string.Empty;
            TextBox6.Text = string.Empty;
            Image1.ImageUrl = string.Empty;
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                //处理图片上传
                string imagePath = "";
                if (FileUpload1.HasFile)
                {
                    Up_img uploader =new Up_img();
                    imagePath = uploader.Upload_img(FileUpload1);
                    if (string.IsNullOrEmpty(imagePath))
                    {
                        Label2.Text = uploader.tips;
                        return;
                    }
                }
                // 设置图片路径参数
                SqlDataSource3.InsertParameters["Image"].DefaultValue = imagePath;

                //执行插入
                SqlDataSource3.Insert();

                //清空输入框并刷新商品列表
                ClearInputs();
                DropDownList2.DataBind();
                Label2.Text = "添加商品成功！";
            }
            catch (Exception ex)
            {
                Label2.Text="添加商品失败："+ ex.Message;
            }
        }

        // 选择商品时加载商品信息
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataView view = (DataView)SqlDataSource3.Select(DataSourceSelectArguments.Empty);
            int selectecIndex = DropDownList2.SelectedIndex;
            DataRowView row = view[selectecIndex];

            // 填充表单
            DropDownList3.SelectedValue = row["CategoryId"].ToString();
            TextBox2.Text = row["ListPrice"].ToString();
            TextBox3.Text = row["UnitCost"].ToString();
            DropDownList4.SelectedValue = row["SuppId"].ToString();
            TextBox4.Text = row["Name"].ToString();
            TextBox5.Text = row["Descn"].ToString();
            TextBox6.Text = row["Qty"].ToString();
            Image1.ImageUrl = row["Image"].ToString();

        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            if(DropDownList2.SelectedValue != null)
            {
                try
                {
                    // 处理图片上传
                    string imagePath="";
                    if (FileUpload1.HasFile)
                    {
                        Up_img uploader = new Up_img();
                        imagePath = uploader.Upload_img(FileUpload1);
                        if (string.IsNullOrEmpty(imagePath))
                        {
                            Label2.Text = uploader.tips;
                            return;
                        }
                    }
                    // 如果上传了新图片，则更新图片路径
                    if (!string.IsNullOrEmpty(imagePath))
                    {
                        SqlDataSource3.UpdateParameters["Image"].DefaultValue= imagePath;
                    }
                    //执行更新
                    SqlDataSource3.Update();
                    // 清空输入框并刷新商品列表
                    ClearInputs();
                    DropDownList2.DataBind();
                    Label2.Text = "修改商品成功！";
                }
                catch (Exception ex)
                {
                    Label2.Text = "修改商品失败："+ex.Message;
                }
            }
            else
            {
                Label2.Text = "请选择要修改的商品！";
            }
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            if (DropDownList2.SelectedValue != null)
            {
                try
                {
                    //执行删除
                    SqlDataSource3.Delete();
                    // 清空输入框并刷新商品列表
                    ClearInputs();
                    DropDownList2.DataBind();
                    Label2.Text = "删除商品成功！";
                }
                catch(Exception ex)
                {
                    Label2.Text = "删除商品失败："+ex.Message;
                }
            }
            else
            {
                Label2.Text = "请选择需要删除的商品！";
            }
        }
    }
}